<?php
/**
* Common Class
*/
class Common
{
    function curPageURL() {
        $pageURL = 'http';
        $pageURL .= "://";
        if ($_SERVER["SERVER_PORT"] != "80") {
            $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
        } else {
            $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
        }
        return $pageURL;
    }

    function replaceCharacter($story, $charOld, $charNew) {
        $newStory = str_replace($charOld, $charNew, $story);
        return $newStory;
    }

    function saveImageWithText($text, $source_file, $public_file_path) {
        //$aryText = $this->ttfWordWrappedText_fixed($text);

        // Copy and resample the imag
        list($width, $height) = getimagesize($source_file);
        $image_p = imagecreatetruecolor($width, $height);
        $image = imagecreatefromjpeg($source_file);
        imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);

        $font = 'images/upload/tahoma.ttf';
        $font_size = 11;

        // Prepare font size and colors
        $text_color = imagecolorallocate($image_p, 0, 0, 0);
        $bg_color = imagecolorallocate($image_p, 255, 255, 255);


        // Set the offset x and y for the text position
        $offset_x = 30;
        $offset_y = 200;

        // Get the size of the text area
        $dims = imagettfbbox($font_size, 0, $font, $text);
        $text_width = $dims[4] - $dims[6] + $offset_x;
        $text_height = $dims[3] - $dims[5] + $offset_y;
        // Add text
        //$this->imagettfJustifytext($image_p, $text, $font, 1, 0, 10, 10, 40, 200, $font_size, $text_color);
        $this->imagettfJustifytext($image_p, $text, $font, 0, 300, 400, $offset_x, $offset_y, $font_size, $text_color);
        //$lines = explode('|', wordwrap($text, 80, '|'));
        /* foreach ($lines as $line)
        {
            imagettftext($image_p, $font_size, 0, $offset_x, $offset_y, $text_color, $font, $line);
            // Increment Y so the next line is below the previous line
            $offset_y += 23;
        } */
        /* foreach ($aryText as $val) {
            foreach ($val as $text) {
                $offset_y = $offset_y + 20;
                imagettftext($image_p, $font_size, 0, $offset_x, $offset_y + 50, $text_color, $font, $text);
            }
        } */
        // Save the picture
        imagejpeg($image_p, $public_file_path, 100);

        // Clear
        imagedestroy($image);
        imagedestroy($image_p);
    }

    function uploadImage($file) {
        $path = "../uploads/";
        $valid_formats = array("jpg", "bmp","jpeg", "png");
        $name = $file['name'];
        if(strlen($name))
        {
            list($txt, $ext) = explode(".", $name);
            if(in_array($ext,$valid_formats) && $file['size'] <= 2560*1024)
            {
                $upload_status = move_uploaded_file($file['tmp_name'], $path.$file['name']);
                if($upload_status){
                    $new_name = time().".jpg";
                    $path_img = $path . $new_name;
                    // Here you have to user functins watermark_text or watermark_image
                    if($this->watermark_image($path.$file['name'], $path_img)) {
                        $demo_image = 'uploads/' . $new_name;
                        return $demo_image;
                    }
                }
            }
            else
                $msg="File size Max 2.56 MB or Invalid file format.";
        }
    }

    function watermark_image($oldimage_name, $new_image_name) {
        $image_path = "../images/TINHNQLOGO100x50.png";
        list($owidth,$oheight) = getimagesize($oldimage_name);
        $width = $height = 300;
        $im = imagecreatetruecolor($width, $height);

        $extension =$this->file_extension($oldimage_name);
        if ($extension == 'png') {
            $img_src = imagecreatefrompng($oldimage_name);
        } else {
            $img_src = imagecreatefromjpeg($oldimage_name);
        }

        imagecopyresampled($im, $img_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
        $watermark = imagecreatefrompng($image_path);
        list($w_width, $w_height) = getimagesize($image_path);
        $pos_x = $width - $w_width;
        $pos_y = $height - $w_height;
        imagecopy($im, $watermark, $pos_x, $pos_y, 0, 0, $w_width, $w_height);
        imagejpeg($im, $new_image_name, 100);
        imagedestroy($im);
        unlink($oldimage_name);
        return true;
    }

    function watermark_text($oldimage_name, $new_image_name)
    {
        $font_path = "../fonts/tahoma.ttf";
        $font_size= 12;
        $water_mark_text_2 = "TinhNQ.com";

        list($owidth,$oheight) = getimagesize($oldimage_name);
        $width = $height = 300;
        $image = imagecreatetruecolor($width, $height);
        $image_src = imagecreatefromjpeg($oldimage_name);
        imagecopyresampled($image, $image_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
        $blue = imagecolorallocate($image, 79, 166, 185);
        imagettftext($image, $font_size, 0, 68, 190, $blue, $font_path, $water_mark_text_2);
        imagejpeg($image, $new_image_name, 100);
        imagedestroy($image);
        unlink($oldimage_name);
        return true;
    }

    function file_extension($filename) {
        $path_info = pathinfo($filename);
        return $path_info['extension'];
    }

    /**
      * Paging in list
      * @author Luvina
      * @access public
      * @param int $total : tong so record
      * @param int $page_curent : trang hiện tại
      * @param string $url_current_ad : dường dẫn trang hiện tại
      * @param int $per_page : so record moi trang
      * @param int $number_paging_group : tong so trang trong 1 group mac dinh la 10
     */
    function paging($total, $page_curent, $url_current_ad, $per_page, $number_paging_group = 10) {
        $total_paging = ceil($total/$per_page);

        if ($total_paging > 1) {
            $total_group = ceil($total_paging/$number_paging_group);
            $curent_group = ceil($page_curent/$number_paging_group);
    
            $end_paging = $curent_group * $number_paging_group;
            if ($total_paging < $end_paging) {
                $end_paging = $total_paging;
            }

            $start_paging = $curent_group * $number_paging_group - $number_paging_group + 1;
            
            $_html = '<div class="pagination small"><ul>';
            
            $html_first = '';
            if ($page_curent == 1) {
                $html_first = "<li class='first disabled'><a><i class='icon-first-2'></i></a></li>";
            } else {
                $html_first = "<li class='first'>" . "<a href='$url_current_ad&page=1'>". '<i class="icon-first-2"></i></a></li>';
            }
            $_html .= $html_first;
            
            $html_prev = '';
            if ($curent_group == 1) {
                $html_prev = '<li class="prev disabled"><a><i class="icon-previous"></i></a></li>';
            } else {
                $next_group = $start_paging - 1;
                $html_prev = '<li class="prev">' . "<a href='$url_current_ad&page=$next_group'>".'<i class="icon-previous"></i></a></li>';
            }
            $_html .= $html_prev;
            
            $html = "";
            for ($i = $start_paging; $i<= $end_paging; $i++) {
                $active = '';
                if ($page_curent == $i) {
                    $html .= "<li class='disabled'><a>$i</a></li>";
                } else {
                    $html .= "<li><a href='$url_current_ad&page=$i'>$i</a></li>";
                }
            }
            $_html .= $html;
            
            $html_next = '';
            if ($curent_group == $total_group) {
                $html_next = '<li class="next disabled"><a><i class="icon-next"></i></a></li>';
            } else {
                $next_group = $end_paging + 1;
                $html_next = '<li class="next">' . "<a href='$url_current_ad&page=$next_group'>".'<i class="icon-next"></i></a></li>';
            }
            $_html .= $html_next;
            
            $html_last = '';
            if ($page_curent == $total_paging) {
                $html_last = "<li class='last disabled'><a><i class='icon-last-2'></i></a></li>";
            } else {
                $html_last = "<li class='last'>" . "<a href='$url_current_ad&page=$total_paging'>". '<i class="icon-last-2"></i></a></li>';
            }
            $_html .= $html_last;
            $_html .= '</ul></div>';
            $_html .= '<small class="text-muted">Tổng số: '. $total .' bản ghi</small>';
            
            return $_html;
        }
        return '';
    }
}
?>